package com.leetcode.LeetCode精选TOP面试题;

/**
 * @author 覃国强
 * @date 2022-07-04 16:54
 */
public class 题库_0155_最小栈 {

  static
  class MinStack {

    private Node head;

    public MinStack() {
      head = new Node();
    }

    public void push(int val) {
      Node node = new Node(val);
      Node next = head.next;
      node.minValue = Math.min(val, next != null ? next.minValue : Integer.MAX_VALUE);
      head.next = node;
      node.next = next;
    }

    public void pop() {
      Node next = head.next;
      head.next = head.next.next;
      next.next = null;
    }

    public int top() {
      return head.next.value;
    }

    public int getMin() {
      return head.next.minValue;
    }


    static class Node {

      int value;

      int minValue;

      Node next;

      Node(){}

      Node(int value) {
        this.value = value;
      }

    }

  }

}
